package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;



public class Util {
	public static String YEAR = "";
	public static String MONTH = "";
	public static String DAY = "";
	static{
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(d);
		String[] _nums = date.split("-"); 
		YEAR = _nums[0];
		MONTH = _nums[1];
		DAY = _nums[2];
	}
	public static String readFile(String fileName) {
		File fr;
		String last = "";
		try {
			String temp = null;
			fr = new File(fileName);
			BufferedReader  br = null;
			br = new BufferedReader(new FileReader (fr));
			while ((temp = br.readLine()) != null) {
				temp = temp.trim();
				last += temp+"\t";
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return last;
	}
	
	public static List<String> readAllFile(String dirName) throws IOException{
		List<String> reList = new ArrayList<String>();
		File file = new File(dirName);
		for (int i = 0; i < file.listFiles().length; i++) {
			File f = file.listFiles()[i];
			reList.add(readFile(dirName+"/"+f.getName()));
		}
		return reList;
	}
	
	
	public static List<String> readXML(String namePath){
		List<String> list = new ArrayList<String>();
		SAXReader reader = new SAXReader(); 
		try {
			Document doc = reader.read(new File(namePath));
			Element ele = doc.getRootElement();
			for(Object childs : ele.elements()){
				Element child = (Element)childs;
				list.add(child.getText());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	
	public static void main(String[] args) throws IOException {
//		readAllFile("txt");
		readXML("E:/My/workspace/AL/OpenFlashChart_0040_showFinash/WebRoot/xml/test.xml");
	}
}
